import time


from selenium.webdriver.common.by import By
from selenium.webdriver.common.devtools.v85.dom_storage import clear
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.Utils import BlogDriver



# 测试博客登录页面
class BlogLogin:
    url = ""
    driver = ""

    def __init__(self):
         self.url="http://8.137.19.140:9090/blog_login.html"
         self.driver=BlogDriver.driver
         self.driver.get(self.url)
         time.sleep(2)

    def Clear(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

    def login(self,username=None,passwpord=None):
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(username)
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(passwpord)

    #成功登录测试页面
    def LoginSucTest(self):
        print("开始执行成功登录测试-----")
        # 清空
        self.Clear()

        self.login("zhangsan","123456")
        BlogDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        # 找到博客首页用户昵称，说明登录成功，否则登录成功
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        # 添加屏幕截图
        BlogDriver.getScreeShot()
        self.driver.back()
    #异常登录测试用例
    def LoginFaulTest(self):
        # 错误账号错误密码
        self.Clear()
        self.login("zhang","123")
        # 添加屏幕截图
        BlogDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()

        #弹窗
        WebDriverWait(self.driver,timeout=3).until(EC.alert_is_present())
        alert=self.driver.switch_to.alert
        alert_text=alert.text
        alert.accept()
        #添加屏幕截图
        BlogDriver.getScreeShot()
        #断言检查是否符合预期
        assert alert_text=="用户不存在"

        #正确账号错误密码
        # 清空
        self.Clear()
        self.login("zhangsan", "123")
        # 添加屏幕截图
        BlogDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 弹窗
        WebDriverWait(self.driver,timeout=3).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        alert.accept()
        # 添加屏幕截图
        BlogDriver.getScreeShot()
        # 断言检查是否符合预期
        assert alert_text == "密码错误"

        # 错误账号正确密码
        self.Clear()
        self.login("zhang", "123456")
        # 添加屏幕截图
        BlogDriver.getScreeShot()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        # 弹窗
        WebDriverWait(self.driver,timeout=3).until(EC.alert_is_present())
        alert = self.driver.switch_to.alert
        alert_text = alert.text
        alert.accept()
        # 添加屏幕截图
        BlogDriver.getScreeShot()
        # 断言检查是否符合预期
        assert alert_text == "用户不存在"


# login=BlogLogin()
# #login.LoginSucTest()
# login.LoginFaulTest()